package com.ks.app.search.action;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.ks.app.admin.action.BaseAction;
import com.ks.app.search.util.RequestUtil;

@ParentPackage("struts-default")
@InterceptorRef("defaultStack")
@Results( {
	@Result(name = "news", location = "/WEB-INF/search/news.jsp")         //搜索结果
})
public class KsNewsAction extends BaseAction{

	private static final long serialVersionUID = 7154204106456077831L;

	private Log log = LogFactory.getLog(this.getClass());
	
	SimpleDateFormat df=new SimpleDateFormat("HH:mm MM-dd"); 
	private String now=df.format(new Date());
	
	private static String nextpage="",prepage="";
	
	public  static String pram="";
	
	public  static String getPram() {
		return pram;
	}

	public static void setPram(String pram) {
		KsNewsAction.pram = pram;
	}
	
	/**
	 * 网页搜索
	 * @return
	 */
	@Action(value="news_search")
	public String newsSearch(){
			
		   // f:第一页，n：下一页，p：上一页
		   String pno       = request.getParameter("pno")==null?"f":request.getParameter("pno").toString().trim();    
		   // 搜索关键字
		   String keyword	= request.getParameter("keyword")==null?"":request.getParameter("keyword").toString().trim();
		   
		   String kutf8  = ""; 
		   String result = "";
		   
		   if("f".equals(pno)){
			   
			   String ver	= request.getParameter("ver")==null?"":request.getParameter("ver").toString().trim();	
			   String src	= request.getParameter("src")==null?"":request.getParameter("src").toString().trim();	
			   String plat	= request.getParameter("plat")==null?"":request.getParameter("plat").toString().trim();	
			   String imsi	= request.getParameter("imsi")==null?"":request.getParameter("imsi").toString().trim();	
			   String clnt	= request.getParameter("clnt")==null?"":request.getParameter("clnt").toString().trim();	
			   String smsc	= request.getParameter("smsc")==null?"":request.getParameter("smsc").toString().trim();		   
			   pram="ver="+ver+"&amp;src="+src+"&amp;plat="+plat+"&amp;imsi="+imsi+"&amp;clnt="+clnt+"&amp;smsc="+smsc;
			
		   }
		   else{	   
			   try {
				    keyword = new String(keyword.getBytes("ISO8859-1"),"utf-8");		
				} catch (Exception e) {
					e.printStackTrace();
				}
		   }
		   
		   request.setAttribute("pram", pram);		
		   
		   try {
		    	kutf8 = URLEncoder.encode(keyword,"utf-8");
		   } catch (Exception e) {
			    log.info(kutf8+""+e.getMessage());
				e.printStackTrace();
		   }
		    
		   if(keyword.trim().length()>0)
		   {	
				keyword=keyword.replace(" ","");
				request.setAttribute("keyword",keyword);
				
				if("f".equals(pno)){
					String url = "http://baidu.mobi/news?from=1292a&st=102041&word="+keyword;
					result = RequestUtil.sendConnRequest(url);
				}else if("n".equals(pno)){
					result = RequestUtil.sendConnRequest(nextpage.replace("&amp;","&"));
				}else if("p".equals(pno)){
					result = RequestUtil.sendConnRequest(prepage.replace("&amp;","&"));
				}
				
				
				if(result.indexOf("<div class=\"reswrap\">抱歉")>-1){
					
		    		result="抱歉,没有找到\""+keyword+"\"的相关内容.<br/>";
		    		request.setAttribute("result", result);
		    		
		    	}else{
		    		
		    		String styles = result.substring(result.indexOf("<style"),result.indexOf("</style")+8);
					if(result.indexOf("重新搜索")<0){
						result="暂无"+keyword+"相关结果,请重新搜索!";
						request.setAttribute("result", result);
						request.setAttribute("styles", styles);
						request.setAttribute("now", now);				
						return "news";
					}
					
					result=result.substring(result.indexOf("重新搜索")+20,result.lastIndexOf("回顶部")-30);
			      	if(result.indexOf("<a href=\"news?")>-1){
						result=result.replaceAll("<a href=\"news?","<a href=\"http://m.baidu.com/ssid=0/from=1292a/bd_page_type=1/news?");			
					}
					if(result.indexOf("<a href=\"t=news/tc")>-1){
						result=result.replaceAll("<a href=\"t=news/tc","<a href=\"http://m.baidu.com/ssid=0/from=1292a/bd_page_type=1/t=news/tc");
					}
					result=result.replaceAll("<a href=\"./","<a href=\"http://m.baidu.com/");
					result=result.replaceAll("</div>","</div><br/>");   
					 
					if(result.indexOf("<div class=\"d\"")>-1)
					{
						String appendnext="",appendpre="";
						if(result.indexOf("下一页</a>")>0)
						{
							String nextpagehref=result.substring(result.indexOf("<div class=\"d\">"),result.indexOf("下一页</a>"));
							nextpage=nextpagehref.substring(24, nextpagehref.length()-2);
							appendnext="<a href=\"news_search?pno=n&keyword="+kutf8+"\">下一页</a>";
						}					
						if(result.indexOf("上一页</a>")>0)
						{
							if(result.indexOf("下一页</a> &#160;")>0)
							{
								String prepagehref=result.substring(result.indexOf("下一页</a> &#160;"),result.indexOf("上一页</a>"));
								prepage=prepagehref.substring(23, prepagehref.length()-2);
								appendpre="<a href=\"news_search?pno=p&keyword="+kutf8+"\">上一页</a>";
							}
							else
							{
								String prepagehref=result.substring(result.indexOf("<div class=\"d\""),result.indexOf("上一页</a>"));
								prepage=prepagehref.substring(24, prepagehref.length()-2);
								appendpre="<a href=\"news_search?pno=p&keyword="+kutf8+"\">上一页</a>";
							}
						}	
						result=result.substring(0,result.indexOf("<div class=\"d\">"))+appendnext.trim()+"  "+appendpre.trim();
					}
					request.setAttribute("result", result);
					request.setAttribute("styles", styles);
		    	}		
		}
		else
		{
			request.setAttribute("result", "请输入关键字！<br/><br/>");
		}
		   
		request.setAttribute("now", now);
		return "news";
	}
	
}
